package com.hexus.eve.actions.security;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.hexus.eve.entities.security.Authorities;
import com.hexus.eve.entities.security.SecurityGroup;
import com.opensymphony.xwork2.ActionSupport;

public class EditGroup extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private SessionFactory sessionFactory;
	private Integer securityGroupId;
	private SecurityGroup securityGroup = new SecurityGroup();
	
	@Override
	public String execute() throws Exception {
		if(securityGroupId != null) {
			HibernateTemplate ht = new HibernateTemplate(sessionFactory);
			securityGroup = ht.get(SecurityGroup.class, securityGroupId);
		}
		return SUCCESS;
	}

	public Integer getSecurityGroupId() {
		return securityGroupId;
	}

	public void setSecurityGroupId(Integer securityGroupId) {
		this.securityGroupId = securityGroupId;
	}

	public SecurityGroup getSecurityGroup() {
		Set<String> a = securityGroup.getAuthorities();
		if(a.size() == 0)
			a.add(Authorities.ROLE_USER.name());
		return securityGroup;
	}

	public void setSecurityGroup(SecurityGroup securityGroup) {
		this.securityGroup = securityGroup;
	}	
	
	public String getName(){
		return securityGroup.getName();
	}
	
	public List<Authorities> getAuthorities() {
		return  Arrays.asList(Authorities.values());
	}

}
